﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using DigtiMatrix.Entity;

namespace DigitMatrix.DataAccess
{
    public class EventTypeDataAccess
    {

        public static DataSet  GetEventByCamercaidVideoFile(Database db, int Camercaid, string VideoFile)
        {
            string cmdText = string.Format("select EventType from SysEvent where CamercaId={0} and videoFile='{1}'", Camercaid, VideoFile);
            try
            {
                cmdText = cmdText.Replace("\r\n", "");
                return db.ExecuteDataSet(CommandType.Text, cmdText);

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        public static int ClearEventByDate(Database db,string startDate,string endDate)
        {
            string cmdText = string.Format("delete from SysEvent where EventDate>='{0}' and EventDate<='{1}'", startDate, endDate);
            try
            {
                cmdText = cmdText.Replace("\r\n", "");
                return db.ExecuteNonQuery(CommandType.Text, cmdText);

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        public static int ClearEvent(Database db)
        {
            string cmdText = string.Format("delete from SysEvent");
            try
            {
                cmdText = cmdText.Replace("\r\n", "");
                return db.ExecuteNonQuery(CommandType.Text, cmdText);

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        public static int Insert(Database db, SysEvent oSysEvent)
        {

            StringBuilder sbField = new StringBuilder();
            StringBuilder sbValue = new StringBuilder();
            sbField.Append("INSERT INTO  SysEvent(");
            sbValue.Append("values(");
            sbField.Append("EventType");
            sbValue.AppendFormat("{0}", oSysEvent.EventType);
            sbField.Append(",EventDescription");
            sbValue.AppendFormat(",'{0}'", oSysEvent.EventDescription);
            sbField.Append(",StartDate");
            sbValue.AppendFormat(",'{0}'", oSysEvent.StartDate);
            sbField.Append(",EndDate");
            sbValue.AppendFormat(",'{0}'", oSysEvent.EndDate);

            sbField.Append(",EventDate");
            sbValue.AppendFormat(",'{0}'", oSysEvent.EventDate);

            sbField.Append(",CamercaId");
            sbValue.AppendFormat(",{0}", oSysEvent.CamercaId);

            sbField.Append(",videoFile");
            sbValue.AppendFormat(",'{0}'", oSysEvent.VideoFile);


            sbField.Append(",Processor)");
            sbValue.AppendFormat(",'{0}')", oSysEvent.Processor);
            string cmdText = sbField.ToString() + " " + sbValue.ToString() ;


            try
            {
                cmdText = cmdText.Replace("\r\n", "");
                return db.ExecuteNonQuery(CommandType.Text, cmdText);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public List<SysEvent> SearchEvent(Database db, int CamercaId, int eventType, string startDate, string endDate)
        {
            string cmdText = "select * from SysEvent where 1=1  ";
            if (CamercaId > 0)
            {
                cmdText = cmdText + " and CamercaId=" + CamercaId.ToString();
            }
            if (eventType > 0)
            {
                cmdText = cmdText + " and EventType=" + eventType.ToString();
            }
            cmdText = cmdText + string.Format(" and EventDate>='{0}' and EventDate<='{1}' order by EventDate", startDate, endDate);

            try
            {
                cmdText = cmdText.Replace("\r\n", "");
                DataSet ds= db.ExecuteDataSet(CommandType.Text, cmdText);
                List<SysEvent> list = new List<SysEvent>();
                SysEvent oSysEvent;
                foreach (DataRow  item in ds.Tables[0].Rows)
                {
                    oSysEvent = new SysEvent(item);
                    list.Add(oSysEvent);
                }
                return list;

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

    }
}
